package com.bw.goods.mvvm.viewmodel;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.goods.R;
import com.bw.goods.mvvm.adpter.HomeGoodsAdapter;
import com.bw.goods.mvvm.bind.GoodsRvBind;
import com.bw.goods.mvvm.model.HomeModel;
import com.bw.goods.mvvm.model.HomeValueModel;
import com.bw.goods.mvvm.model.entity.CarEntity;
import com.bw.goods.mvvm.model.entity.HomeGoodsEntity;
import com.bw.greendaomoudle.green.DaoSessionUtils;
import com.bw.mvvm_comom.BaseApplication;
import com.bw.mvvm_comom.entity.BaseEntity;
import com.bw.mvvm_comom.filed.UIFiled;
import com.bw.mvvm_comom.mvvm.BaseViewModel;
import com.bw.user.mvvm.db.UserEntityValues;
import com.bw.user.mvvm.model.entity.UserDaoEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomeValueViewModel extends BaseViewModel<HomeValueModel> {

    public ObservableField<Boolean> refresh = new ObservableField<>(false);
    public ObservableField<Boolean> load = new ObservableField<>(false);
    public ObservableField<HomeGoodsEntity.DataBean> entity = new ObservableField<>();
    public HomeGoodsAdapter adapter = new HomeGoodsAdapter();
    private int pageNo = 1;
    private CompositeDisposable compositeDisposable;
    @Override
    public void onCreate() {
        super.onCreate();


        HashMap<String,Object> map = new HashMap<>();
        map.put("categoryId",14);
        map.put("pageNo",pageNo);
        m.requestGoods(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);

    }

    public void finishActivity(){
        getUiChange().getFinishActObServAble().setValue(""+System.currentTimeMillis());
    }

    public void share(){

    }

    public void carList(){
    }

    public void addCar(){
//        {
//            "goodsCount": 0,
//                "goodsDesc": "string",
//                "goodsIcon": "string",
//                "goodsId": 0,
//                "goodsPrice": "string",
//                "goodsSku": "string",
//                "time": 0,
//                "userId": 0
        SharedPreferences sj = BaseApplication.getApplication().getSharedPreferences("sj", Context.MODE_PRIVATE);
        HashMap<String,Object> map = new HashMap<>();
        map.put("time",""+System.currentTimeMillis()/1000);
        map.put("userId",sj.getInt("id",1));
        map.put("goodsSku",sj.getString("dec",""));
        map.put("goodsPrice",sj.getString("price",""));
        map.put("goodsId",sj.getInt("id",1));
        map.put("goodsIcon",sj.getString("img",""));
        map.put("goodsDesc",sj.getString("title",""));
        map.put("goodsCount",1);
        m.requestAddCar(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);

        getUiChange().getFinishActObServAble().setValue("" + System.currentTimeMillis());
        Intent intent = new Intent();
        intent.setAction("buycar");
        BaseApplication.getApplication().sendBroadcast(intent);

    }
//    public void refreshListener(){
//        refresh.set(true);
//        pageNo = 1;
//        HashMap<String,Object> map = new HashMap<>();
//        map.put("categoryId",14);
//        map.put("pageNo",pageNo);
//        m.requestGoods(map)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(this);
//    }
//    public void loadMoreListener(){
//        load.set(true);
//        pageNo += 1;
//        HashMap<String,Object> map = new HashMap<>();
//        map.put("categoryId",14);
//        map.put("pageNo",pageNo);
//        m.requestGoods(map)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<BaseEntity>() {
//                    @Override
//                    public void onSubscribe(@NotNull Disposable d) {
//                        compositeDisposable.add(d);
//                    }
//
//                    @Override
//                    public void onNext(@NotNull BaseEntity baseEntity) {
//                        if (baseEntity instanceof HomeGoodsEntity){
//                            HomeGoodsEntity homeGoodsEntity = (HomeGoodsEntity) baseEntity;
//                            if (homeGoodsEntity.getData()==null){
//                                showMsg("当前最后一页");
//                                return;
//                            }
//                            if (homeGoodsEntity.getData().size()==0){
//                                showMsg("当前最后一页");
//                                return;
//                            }
//
//                            adapter.addData(homeGoodsEntity.getData());
//                        }
//                    }
//
//                    @Override
//                    public void onError(@NotNull Throwable e) {
//                        showMsg(""+e.getMessage());
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        getUiChange().getHideDialogObservable().setValue(""+System.currentTimeMillis());
//                        load.set(false);
//                    }
//                });
//
//    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof  HomeGoodsEntity){
            refresh.set(false);
            HomeGoodsEntity homeGoodsEntity = (HomeGoodsEntity) baseEntity;
            adapter.setNewInstance(homeGoodsEntity.getData());
        }


//        if (baseEntity instanceof CarEntity){
//                CarEntity carEntity = (CarEntity) baseEntity;
//                LogUtils.e("有毛病"+carEntity.message);
//                if (carEntity.message.equals("添加购物车成功")){
//                    getUiChange().getFinishActObServAble().setValue("" + System.currentTimeMillis());
//                    Intent intent = new Intent();
//                    intent.setAction("buycar");
//                    BaseApplication.getApplication().sendBroadcast(intent);
//                }
//        }

    }
}
